package api.expertInfo;


import com.baidu.unbiz.fluentvalidator.ComplexResult;
import com.baidu.unbiz.fluentvalidator.FluentValidator;
import com.baidu.unbiz.fluentvalidator.ResultCollectors;
import common.BaseController;
import common.CommonResult;
import common.validateUser.InterfaceUsers;
import common.validateUser.ValidateUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import services.business.BusinessInfoServices;
import services.business.InspectorInfoServices;
import services.dictionary.ItemTypeServices;
import services.dictionary.MajorTypeServices;
import services.expertInfo.*;
import services.staff.StaffInfoServices;
import services.upms.AccountServices;
import util.cryptogram.MD5Util;
import validator.NotBlankFluentValidator;
import validator.NotNullFluentValidator;
import wt.entity.expertInfo.ExpertBase;
import wt.entity.upms.Account;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/8/16.
 */
@Controller
@RequestMapping("httpApi")
public class VerifyExpertAccountController extends BaseController {
    @Autowired
    public InterfaceUsers interfaceUsers;
    @Autowired
    ItemTypeServices itemTypeServices;
    @Autowired
    BusinessInfoServices businessInfoServices;
    @Autowired
    StaffInfoServices staffInfoServices;
    @Autowired
    MajorTypeServices majorTypeServices;
    @Autowired
    ExpertBaseServices expertBaseServices;
    @Autowired
    InspectorInfoServices InspectorInfoServices;
    @Autowired
    ExpertCertificateServices expertCertificateServices;
    @Autowired
    ExpertCompanyServices expertCompanyServices;
    @Autowired
    ExpertWorkServices expertWorkServices;
    @Autowired
    ExpertEducationServices expertEducationServices;
    @Autowired
    ExpertReviewServices expertReviewServices;
    @Autowired
    ExpertTrainServices expertTrainServices;
    @Autowired
    ExpertMajorServices expertMajorServices;
    @Autowired
    AccountServices accountServices;


    /**
     * 获取专家的详细信息
     *
     * @param uid
     * @param timeStamp
     * @param sign
     * @param loginName
     * @param password
     * @param baseInfo
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "verifyExpertAccount", produces = "application/json;charset=UTF-8")
    public CommonResult verifyExpertAccount(
            String uid,
            Long timeStamp,
            String sign,
            String loginName,
            String password,
            String baseInfo
    ) throws Exception {


        ComplexResult complexResult = FluentValidator.checkAll().failOver()
                .on(uid, new NotBlankFluentValidator("uid"))
                .on(timeStamp, new NotNullFluentValidator("timeStamp"))
                .on(sign, new NotBlankFluentValidator("sign"))
                .on(loginName, new NotBlankFluentValidator("loginName"))
                .on(password, new NotBlankFluentValidator("password"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!complexResult.isSuccess()) {
            CommonResult commonResult = new CommonResult();
            commonResult.addMessage(complexResult);
            commonResult.setCode(CommonResult.ZJ005);
            return commonResult;
        }


        CommonResult commonResult = ValidateUserUtil.validateUser(uid, timeStamp, sign, interfaceUsers);  //用户验证
        if (!CommonResult.ZJ001.equals(commonResult.getCode())) {
            return commonResult;
        }


        Account account = accountServices.selectByLoginName(loginName);
        if (account == null) {
            commonResult.setMessage("账号不存");
            commonResult.setCode(CommonResult.ZJ005);
            return commonResult;
        }

        if (!account.getPassword().equalsIgnoreCase(MD5Util.md5Hex(password))) {
            commonResult.setMessage("密码错误");
            commonResult.setCode(CommonResult.ZJ005);
            return commonResult;
        }

        ExpertBase expertBase = expertBaseServices.selectByPrimaryKey(account.getExpertId());
        Map map = new HashMap();
        map.put("name", expertBase.getName());
        map.put("idCard", expertBase.getIdCard());
        map.put("phone", expertBase.getPhone());
        map.put("email", expertBase.getEmail());
        commonResult.setData(map);
        commonResult.setMessage(CommonResult.ZJ001_M);
        commonResult.setCode(CommonResult.ZJ001);
        return commonResult;
    }


}
